package com.xukai.springboot.controller.account;

import com.xukai.springboot.entity.patient.t_patient;
import com.xukai.springboot.entity.request.account.AccountRequest;
import com.xukai.springboot.entity.request.account.WXAccountRequest;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.response.account.AccountResponse;
import com.xukai.springboot.entity.response.account.WXAccountResponse;
import com.xukai.springboot.service.AccountService;
import com.xukai.springboot.service.patient.PatientService;
import com.xukai.springboot.service.patient.PatientWechatService;
import com.xukai.springboot.util.session.SessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(basePath = "account", description = "用户身份认证")
@RestController
@RequestMapping("account/*")
public class AccountController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(AccountController.class);

    /**
     * 登录服务
     */
    @Autowired
    private AccountService accountService;

    /**
     * session服务
     */
    @Autowired
    private SessionService sessionService;

    /**
     * 患者微信服务
     */
    @Autowired
    private PatientWechatService patientWechatService;


    /**
     * 患者信息服务
     */
    private PatientService patientService;

    /**
     * 微信登录验证
     */
    @ApiOperation(value = "微信登录验证", notes = "微信登录验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Param", value = "微信登录验证", required = true, dataType = "WXAccountRequest", paramType = "body")
    })
    @PostMapping("WXAccount")
    public ResponseBase<WXAccountResponse> WXAccount(@RequestBody WXAccountRequest Param) {
        ResponseBase<WXAccountResponse> result = new ResponseBase<>();
        try {
            //验证微信用户信息
            result.setData(accountService.WXAccount(Param.getCode()));
            //判断验证是否通过
            if (result.getData() == null) {
                result.setStatue(0);
                result.setErrMessage("微信用户信息获取失败");
                return result;
            }
            //缓存数据

            sessionService.setCurrentUser(result.getData());

        } catch (Exception e) {
            logger.error("微信登录验证", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 完善患者信息
     */
    @ApiOperation(value = "完善患者信息", notes = "完善患者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Param", value = "患者信息", required = true, dataType = "WXAccountRequest", paramType = "body")
    })
    @PostMapping("CompletionPatient")
    public ResponseBase CompletionPatient(@RequestBody t_patient Param) {
        ResponseBase result = new ResponseBase();
        try {
            patientService.CreatePatient(Param);
            result.setData(Param);
        } catch (Exception e) {
            logger.error("完善患者信息", e);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 管理端用户登录
     */
    @ApiOperation(value = "管理端用户登录", notes = "管理端用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Param", value = "微信登录验证参数", required = true, dataType = "AccountRequest", paramType = "body")
    })
    @PostMapping("Account")
    public ResponseBase<AccountResponse> Account(@RequestBody AccountRequest Param) {
        ResponseBase<AccountResponse> result = new ResponseBase<>();
        try {

            //根据登录名和密码获取用户信息
            AccountResponse data = accountService.Account(Param.getLoginName(), Param.getLoginPassword());
            if (data == null) {
                result.setStatue(0);
                result.setErrMessage("用户名或密码错误");
                return result;
            }
            result.setData(data);
            //创建token并缓存数据
            sessionService.setCurrentUser(result.getData());


        } catch (Exception e) {
            logger.error("管理端用户登录", e);
            result.setStatue(0);
            result.setErrMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 退出登录
     */
    @ApiOperation(value = "退出系统", notes = "退出系统")
    @GetMapping("LoginOut")
    public ResponseBase LoginOut() {
        ResponseBase result = new ResponseBase();
        try {
            boolean loginOutResult = sessionService.removeSession();
            if (!loginOutResult) {
                result.setStatue(0);
                result.setErrMessage("退出登录失败");
            }
        } catch (Exception ex) {
            logger.error("退出登录", ex);
            result.setStatue(0);
            result.setErrMessage("退出登录失败");
        }
        return result;
    }

}
